package main

import (
	"encoding/json"
	"fmt"
	"orangelua"
	"orangelua/vm"
)

type TestStruct struct {
	Name string
	Id   int
	Age  int64
	Sex  bool
}

func main() {
	ts := new(TestStruct)
	ts.Name = "张三"
	ts.Age = 15
	ts.Id = 100
	ts.Sex = false

	luavm := orangelua.GetLua()
	//defer orangelua.FreeLua(luavm)
	luavm.SetGStr("test", "123123")
	luavm.SetGTableForStruct("person", ts)
	luavm.RunLua()
	fmt.Println(luavm.GetGStr("test"))
	luavm.GetGTable2Struct("person", ts)
	data, _ := json.Marshal(ts)
	fmt.Println(string(data))

	orangelua.FreeLua(luavm)

	ts2 := new(TestStruct)
	luavm2 := orangelua.GetLua()
	luavm2.GetGTable2Struct("person", ts2)

	data2, _ := json.Marshal(ts)
	fmt.Println(string(data2))

	// 存在全局变量被污染的情况
	vm.LuaVM.Shutdown()
}
